<?php
/**
 * 地址管理
 * Created by PhpStorm.
 * User: qinzhi
 * Date: 2017/6/30
 * Time: 12:10
 */
class Service_Address{

    /**
     * 获取收货地址
     * @param $uid
     * @return array|bool|mixed|null|string
     */
    public static function getAddressList($uid){
        if($uid <= 0) return false;
        $key = "user_address_{$uid}";
        $dataList = FCache::get($key);
        if(empty($dataList)){
            $addessTable = new FTable('user_address','','xhs_slave');
            $dataList = $addessTable->where(array('uid'=>$uid))->order('is_default,auto_id desc')->select();
            if($dataList) FCache::set($key,$dataList,1800);
        }
        return $dataList;
    }

    /**
     * 清空缓存
     * @param $uid
     */
    public static function clearAddressListCache($uid){
        $key = "user_address_{$uid}";
        FCache::delete($key);
    }

    /**
     * 编辑收货地址
     * @param $id
     * @param $uid
     * @param $contact
     * @param $phone
     * @param $province
     * @param $city
     * @param $county
     * @param $address
     * @param $sex
     * @param string $lat 纬度(y)
     * @param string $lng 经度(x)
     * @param int $is_default
     * @return array|bool   is_default为1是默认
     */
    public static function saveAddress($id,$uid,$contact,$phone,$city,$province,$county,$address,$housemember,$sex,$lat,$lng,$is_default=1){
        $addessTable = new FTable('user_address','','xhs_master');
        $addressList = self::getAddressList($uid);
        if(count($addressList) >= 10) return false;
        if($is_default == 1) FDB::query("UPDATE user_address SET is_default=2 WHERE uid={$uid} and is_default=1",'xhs_master');
        $data = array(
            'uid' => $uid,
            'contact' => $contact,
            'phone' => $phone,
            'province' => $province,
            'city' => $city,
            'county' => $county,
            'address' => $address,
            'housemember' => $housemember,
            'sex' => $sex,
            'lat' => $lat,
            'lng' => $lng,
            'is_default' => $is_default,
            'create_time' => date('Y-m-d H:i:s'),
        );
        if($id > 0){
            self::clearAddressDetailCache($id);
            $addessTable->where(array('auto_id'=>$id,'uid'=>$uid))->update1($data);
            $data['auto_id'] = $id;
        }else
            $data['auto_id'] = $addessTable->insert($data);

        self::clearAddressCache($uid);
        self::clearAddressListCache($uid);
        return $data;
    }

    /**
     * 删除收货地址
     * @param $id
     * @return bool
     */
    public static function delAddress($uid,$id){
        $addessTable = new FTable('user_address','','xhs_slave');
        $address = $addessTable->where(array('auto_id'=>$id,'uid'=>$uid))->find();
        $addessTable = new FTable('user_address','','xhs_master');
        $addessTable->where(array('auto_id'=>$id,'uid'=>$uid))->remove(true);
        self::clearAddressCache($uid);
        self::clearAddressListCache($uid);
        if($address['is_default'] == 1){
            return self::resetDefaultAddress($uid);
        }
        return true;
    }

    /**
     * 重置默认地址
     * @param $uid
     * @return mixed
     */
    public static function resetDefaultAddress($uid){
        $addessTable = new FTable('user_address','','xhs_slave');
        $address = $addessTable->where(array('uid'=>$uid))->order('auto_id desc')->find();
        $addessTable = new FTable('user_address','','xhs_master');
        $addessTable->where(array('auto_id'=>$address['auto_id']))->update1(array('is_default'=>1));
//        self::clearAddressCache($uid);
//        self::clearAddressListCache($uid);
        return $address['auto_id'];
    }

    /**
     * 设置默认地址
     * @param $id
     * @param $uid
     * @return bool
     */
    public static function resetDefaultAddressId($id,$uid){
        $addessTable = new FTable('user_address','','xhs_master');
        $addessTable->where(array('uid'=>$uid,'is_default'=>1))->update1(array('is_default'=>2));
        $addessTable->where(array('auto_id'=>$id,'uid'=>$uid))->update1(array('is_default'=>1));
        self::clearAddressCache($uid);
        self::clearAddressListCache($uid);
        return true;
    }

    /**
     * 获取地址详情
     * @param $id
     * @return bool|array
     */
    public static function getAddress($id){
        $key = "user_address_detail_$id";
        $address = FCache::get($key);
        if(empty($address)){
            $addessTable = new FTable('user_address','','xhs_slave');
            $address = $addessTable->where(array('auto_id'=>$id))->find();
            if($address) FCache::set($key,$address,120);
        }
        return $address;
    }

    /**
     * 清空地址详情缓存
     * @param $id
     */
    public static function clearAddressDetailCache($id){
        $key = "user_address_detail_$id";
        FCache::delete($key);
    }

    /**
     * 获取省份列表
     * @return array
     */
    public static function getProvinceList(){
        $data = array(
            18 => array('provinceId'=>'430000','province'=>'湖南省')
        );
        return $data;
    }

    /**
     * 获取市列表
     * @param $provinceId
     * @return array
     */
    public static function getCityList($provinceId = '430000'){
        $data = array(
            '430000' => array(
                186 => array('cityId'=>'430100','city'=>'长沙市','father'=>'430000')
            )
        );
        return $data[$provinceId];
    }

    /**
     * 获取区域列表
     * @param $cityId
     * @return array
     */
    public static function getCountyList($cityId = '430100'){
        $data = array(
            '430100' => array(
                1796 => array('countyId'=>'430102','county'=>'芙蓉区','father'=>'430100'),
                1797 => array('countyId'=>'430103','county'=>'天心区','father'=>'430100'),
                1798 => array('countyId'=>'430104','county'=>'岳麓区','father'=>'430100'),
                1799 => array('countyId'=>'430105','county'=>'开福区','father'=>'430100'),
                1800 => array('countyId'=>'430111','county'=>'雨花区','father'=>'430100'),
                1801 => array('countyId'=>'430121','county'=>'长沙县','father'=>'430100'),
                1802 => array('countyId'=>'430122','county'=>'望城县','father'=>'430100'),
                1803 => array('countyId'=>'430124','county'=>'宁乡县','father'=>'430100'),
                1804 => array('countyId'=>'430181','county'=>'浏阳市','father'=>'430100'),
            )
        );
        return $data[$cityId];
    }

    /**
     * 获取默认收货地址
     */
    public static function getDefaultAddress($uid){
        $key = "user_address_default_{$uid}";
        $data = FCache::get($key);
        if(empty($data)){
            $addessTable = new FTable('user_address','','xhs_slave');
            $data = $addessTable->where(array('uid'=>$uid,'is_default'=>1))->find();
            if($data) FCache::set($key,$data,1800);
        }
        return $data;
    }

    /**
     * 清空缓存
     * @param $uid
     */
    public static function clearAddressCache($uid){
        $key = "user_address_default_{$uid}";
        FCache::delete($key);
    }
}